package com.junxonline.plat.controller.charge;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.charge.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: ChargeController
 * @Description: 收费控制器
 */

@RestController
@RequestMapping(value = "/admin/charge")
public class ChargeController extends BaseController {

    @Autowired
    private ChargeService chargeService;
    
    /**
     * 获取收费数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getCharge")
    public ResultVO getCharge(@RequestBody QueryChargeVO vo) {

        if (CommonUtils.isEmpty(vo.getChargeId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(chargeService.getCharge(vo));
    }

    /**
     * 筛选查询收费数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryCharge")
    public PageResultVO queryCharge(PageVO page, QueryChargeVO vo) {
        return getReturnSuccess(chargeService.queryCharge(page, vo));
    }


    /**
     * 添加收费
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addCharge")
    public ResultVO addCharge(@RequestBody ChargeVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = chargeService.addCharge(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改收费
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editCharge")
    public ResultVO editCharge(@RequestBody ChargeVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getChargeId()) || CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = chargeService.editCharge(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除收费
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delCharge")
    public ResultVO delCharge(@RequestBody ChargeIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] chargeIdsArray = vo.getChargeIds();

        boolean successMenu = chargeService.delCharge(chargeIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 获取收费类别数据
     * @return
     */
    @RequestMapping(value = "/getCategory")
    public ResultVO getCategory() {
        return getReturnSuccess(chargeService.getCategory());
    }

    /**
     * 获取住户数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getHousehold")
    public ResultVO getHousehold(@RequestBody QueryHouseholdVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseholdName())) {
            return getReturnSuccess();
        }

        return getReturnSuccess(chargeService.getHousehold(vo));
    }

}
